package com.flyme.module.exam.provider.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.exam.client.entity.ExamRecord;
import com.flyme.module.exam.provider.mapper.ExamRecordMapper;
import com.flyme.module.exam.provider.service.ExamRecordService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 答题记录管理接口实现
 */
@Service
public class ExamRecordServiceImpl extends BaseServiceImpl<ExamRecordMapper, ExamRecord> implements ExamRecordService {

    /**
     * 根据用户查询最新的测试记录
     */
    public ExamRecord findByUser(Long accountId) {
        List<ExamRecord> list = list(q().eq("accountInfoId", accountId).orderByDesc("createTime"));
        if (list.size() < 1) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 查询该用户该项目的最后一次答题记录
     */
    public ExamRecord findMax(Long accountId, String abilityItemId) {
        List<ExamRecord> list = list(q().eq("accountInfoId", accountId).eq("abilityItemId", abilityItemId).orderByDesc("testNum"));
        if (list.size() < 1) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 根据用户查询记录
     */
    public ExamRecord findMax2(Long accountId) {
        List<ExamRecord> list = list(q().eq("accountInfoId", accountId));
        if (list.size() < 1) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 根据用户统计用户测评次数
     */
    public Integer countByUser(Long accountId) {
        Integer testNum = count(q().eq("accountInfoId", accountId));
        return testNum;
    }

    /**
     * 查询用户最后一次测评记录
     */
    public ExamRecord findLast(Long userId){
        List<ExamRecord> list = list(q().eq("accountInfoId", userId).orderByDesc("createTime"));
        if(list.size()<1){
            return null;
        }
        return list.get(0);
    }

    /**
     * 查询该用本周测评次数
     */
    public Integer countByWeek(Long userId, DateTime begin, DateTime end){
        return count(q().eq("accountInfoId", userId).ge("createTime", begin).le("createTime", end));
    }

    /**
     * 统计本月测试天数
     */
    public Integer countByMonth(Long userId){
        DateTime begin = DateUtil.beginOfMonth(DateUtil.date());
        DateTime end = DateUtil.endOfMonth(DateUtil.date());
        List<ExamRecord> list = list(q().eq("accountInfoId", userId).ge("createTime", begin).le("createTime", end).groupBy("LEFT(createTime,10)"));
        if(ObjectUtils.isNotEmpty(list)){
            return list.size();
        }
        return 0;
    }

    @Override
    public List<EntityMap> totalScore(String createTime, Long userId) {
        EntityMap map = new EntityMap();
        map.put("createTime", createTime);
        map.put("userId", userId);
        return baseMapper.totalScore(map);
    }


}
